package com.tangtanwei.hotel.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangtanwei.hotel.entity.Room;
import com.tangtanwei.hotel.entity.RoomNumber;
import com.tangtanwei.hotel.entity.User;
import com.tangtanwei.hotel.entity.UserRoomNumber;
import com.tangtanwei.hotel.entity.enums.RoomStatusEnums;
import com.tangtanwei.hotel.mapper.RoomNumberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class RoomNumberService extends ServiceImpl<RoomNumberMapper, RoomNumber> {

    @Autowired
    private UserRoomNumberService userRoomNumberService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @Transactional(rollbackFor = Exception.class)
    public synchronized String post(String roomId, String userId) {
        RoomNumber byId = super.getById(roomId);
        if (byId == null)
            return "房间号不存在!!请重试";

        if (byId.getStatus().equals(RoomStatusEnums.Full.value))
            return "该房间已被占用! ";

        byId.setStatus(RoomStatusEnums.Full.value);
        super.saveOrUpdate(byId);

        User user = userService.getById(userId);
        UserRoomNumber build = UserRoomNumber.builder().createTime(LocalDateTime.now())
                .userId(userId).roomNumberId(roomId).name(user.getName()).phone(user.getPhone())
                .idcard(user.getIdcard()).birthday(user.getBirthday()).build();


        userRoomNumberService.saveOrUpdate(build);

        // 对应房间类型id的房间数量 减 1
        Room room = roomService.getById(byId.getParentId());
        if (room != null){
            Integer count = room.getCount();
            if (count <= 0){
                room.setState(RoomStatusEnums.Full.value);
            }else {
                room.setCount(count - 1);
            }
            roomService.saveOrUpdate(room);
        }
        return "分配成功";
    }

    @Transactional(rollbackFor = Exception.class)
    public String del(String roomId, String userId) {
        LambdaQueryWrapper<UserRoomNumber> wrapper = new QueryWrapper<UserRoomNumber>().lambda()
                .eq(UserRoomNumber::getRoomNumberId, roomId).eq(UserRoomNumber::getUserId, userId);
        UserRoomNumber one = userRoomNumberService.getOne(wrapper);

        if (one == null)
            return "对不起该用户并没有占用该房间号";

        userRoomNumberService.remove(wrapper);

        RoomNumber byId = super.getById(roomId);
        if (byId == null)
            return "对不起该房间号不存在无法退房";

        if (byId.getStatus().equals(RoomStatusEnums.FREE.value))
            return "该房间已经是空闲无法退房";

        byId.setStatus(RoomStatusEnums.FREE.value);
        super.saveOrUpdate(byId);
        return "用户"+userId+"退房成功";

    }
}
